--[[
VHeWS example of a simple sensor firmware as concept test.

This Code is part of the graduation project of Pedro Bittencourt e Silva,
VHeWS.
Developed to be used with eLua0.5 on LM3S8962.

@author Pedro Bittencourt
--]]

--configure AD converter
--[[timer = 0
adcchannels = {0,1,2,3}
adcsmoothing = {4, 16, 32, 64}

for i, v in ipairs(adcchannels) do
	adc.setblocking(v,0)
	adc.setsmoothing(v,adcsmoothing[i])
	adc.setclock(v, 30 ,timer)
end  
adc.sample(adcchannels,128)
]]
function functable(t)
	s=""
	for k,v in pairs(t) do
		s = s..tostring(v)..","
	end
	return s
end
	
function sserialize(t,sep,first)
	s = ""
	if first == nil then first = true end
	for k,v in pairs(t) do
		if type(v) ~= "table" then
			if first then
				s=s..tostring(k).."="..tostring(v)
				first = false
			else
				s=s..sep..tostring(k).."="..tostring(v)
			end
		else
			if first then
				s=s..tostring(k).."="..functable(v)
				first = false
			else
				s=s..sep..tostring(k).."="..functable(v)
			end
		end
	end
	return s
end

local REGPORTNUM = 8080          --port to connect to registry server
local REGHOST = "192.168.0.101"  --registry server host IP
local DATAPORTNUM = 8080         --port to connect to data server
local DATAHOST = "192.168.0.101" --data server host IP

--Definition of description package
local IDPackage = {
	Name = "VHeWS%20demo",
	IP = "192.168.0.105",
	Desc = "Sensor%20de%20exemplo%20do%20VHeWS,%20leitura%20do%20ADC",
	FuncNames = {},
}

--[[
readAD function that will be polled

@return a numeric value read by the ADC
--]]
function readAD()
--[[	for i, v in ipairs(adcchannels) do
    tsample = getsample(v)
    if not (tsample == nil) then
		res = tsample
    end
    if adc.isdone(v) == 1 then adc.sample(adcchannels,128) end
  end--]]
	return 0 --##dummy value by now, check AD lib by JSnyder 
end

table.insert(IDPackage.FuncNames,"readAD");

function formatRequest(to,s,host,port)
	local res = "GET /"..to.."?"..s.." HTTP/1.1\r\nHost: "..host..":"..port.."\r\nConnection: close\r\n\r\n"
	return res
end

local payload = formatRequest("registry.lua",sserialize(IDPackage,"&"),REGHOST,REGPORTNUM)

repeat
	local sock = net.socket(0)
	local err_con = net.connect(sock,net.packip(REGHOST),REGPORTNUM)
	print("connection error num:"..tostring(err_con))
	sentBytes,err_send = net.send(sock,payload)
	net.close(sock)
until err_con == 0

if err_sen ~= 0 then
	print("connection error num:"..tostring(err_con))
end

--enter on "server mode" to receive requests from Data services
while true do
	local sock, remoteip, err = net.accept(80)
	print("Got connection on socket", sock )
	print("Remote ip: " .. net.unpackip(remoteip, "*s"))
	local request, err_recv = net.recv(sock, 1024)
	for k in string.gmatch(request,"GET /(.+) HTTP") do
		fname = string.match(k,"fname=(.+)")
		if fname == "readAD" then
			res = readAD()
			local payload = "HTTP/1.1 200 OK\r\nConnection: close\r\nServer: eLua-miniweb\r\nContent-Type: txt\r\n\r\n"..res
			sentBytes,err_send = net.send(sock,payload)
		else
			local payload = "HTTP/1.1 200 OK\r\nConnection: close\r\nServer: eLua-miniweb\r\nContent-Type: txt\r\n\r\nFAIL"
			sentBytes,err_send = net.send(sock,payload)
		end
	end
	net.close(sock)
	reqdata = {}
	collectgarbage('collect')
end




	
	

